﻿using Business.Base.Enums;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.CentrifugalPump.Perf
{
    /// <summary>
    /// 设置判定标准
    /// </summary>
    public class StandLevelJudger
    {
        static Hashtable headLevelTable = null;
        static Hashtable flowLevelTable = null;
        static Hashtable powerLevelTable = null;
        static Hashtable effLevelTable = null;

        static StandLevelJudger()
        {
            initDic();
        }

        /// <summary>
        /// 初始化流量，扬程，功率，效率标准
        /// </summary>
        public static void initDic()
        {

            flowLevelTable = new Hashtable();
            flowLevelTable.Add(FlowHeadLevel.FlowHeadLevel1B, 5);
            flowLevelTable.Add(FlowHeadLevel.FlowHeadLevel2B, 8);
            flowLevelTable.Add(FlowHeadLevel.FlowHeadLevel3B, 9);

            headLevelTable = new Hashtable();
            headLevelTable.Add(FlowHeadLevel.FlowHeadLevel1B, 3);
            headLevelTable.Add(FlowHeadLevel.FlowHeadLevel2B, 5);
            headLevelTable.Add(FlowHeadLevel.FlowHeadLevel3B, 7);

            powerLevelTable = new Hashtable();
            powerLevelTable.Add(FlowHeadLevel.FlowHeadLevel1B, 4);
            powerLevelTable.Add(FlowHeadLevel.FlowHeadLevel2B, 8);
            powerLevelTable.Add(FlowHeadLevel.FlowHeadLevel3B, 9);

            effLevelTable = new Hashtable();
            effLevelTable.Add(FlowHeadLevel.FlowHeadLevel1B, 4);
            effLevelTable.Add(FlowHeadLevel.FlowHeadLevel2B, 8);
            effLevelTable.Add(FlowHeadLevel.FlowHeadLevel3B, 9);

        }


        /// <summary>
        /// 判断计算出的扬程是否符合指定标准的要求
        /// </summary>
        /// <param name="head">计算扬程</param>
        /// <param name="ratedHead">额定扬程</param>
        /// <param name="level">判断等级</param>
        /// <returns></returns>
        public static Boolean JudgeHeadLevel(Double head, Double ratedHead, FlowHeadLevel level)
        {
            int rate = (int)headLevelTable[level];
            if (((ratedHead * (100 + rate) / 100.0f) - head) > Double.Epsilon && (head - (ratedHead * (100 - rate) / 100.0f) > Double.Epsilon))
            {
                return true;
            }
            return false;

        }


        /// <summary>
        /// 判断计算出的流量是否符合指定标准的要求
        /// </summary>
        /// <param name="flow">计算流量</param>
        /// <param name="ratedFlow">额定流量</param>
        /// <param name="level">判断等级</param>
        /// <returns></returns>
        public static Boolean JudgeFlowLevel(Double flow, Double ratedFlow, FlowHeadLevel level)
        {
            int rate = (int)flowLevelTable[level];
            if (((ratedFlow * (100 + rate) / 100.0f) - flow) > Double.Epsilon && (flow - (ratedFlow * (100 - rate) / 100.0f) > Double.Epsilon))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 判断计算出的功率是否符合指定标准的要求
        /// </summary>
        /// <param name="power">功率</param>
        /// <param name="ratedPower">额定功率</param>
        /// <param name="level">等级</param>
        /// <returns></returns>

        public static Boolean JudgePowerLevel(Double power, Double ratedPower, FlowHeadLevel level)
        {
            int rate = (int)powerLevelTable[level];
            if (((ratedPower * (100 + rate) / 100.0f) - power) > Double.Epsilon)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 判断计算出的效率是否符合指定标准的要求
        /// </summary>
        /// <param name="eff">效率</param>
        /// <param name="ratedEff">额定效率</param>
        /// <param name="level">等级</param>
        /// <returns></returns>
        public static Boolean JudgeEffLevel(Double eff, Double ratedEff, FlowHeadLevel level)
        {
            int rate = (int)effLevelTable[level];
            if ((eff - ratedEff * (100 - rate) / 100.0f) > Double.Epsilon)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 根据额定流量和扬程的坐标，给出指定标准下的流量扬程范围
        /// </summary>
        /// <param name="flow">流量</param>
        /// <param name="head">扬程</param>
        /// <param name="level">标准</param>
        /// <returns></returns>
        public static Double[,] GetFlowHeadRange(Double flow, Double head, FlowHeadLevel level)
        {
            int flowRate = (int)flowLevelTable[level];
            int headHead = (int)headLevelTable[level];


            double startFlow = flow * (1.0f - flowRate / 100.0f);
            double endFlow = flow * (1.0f + flowRate / 100.0f);

            double startHead = head * (1.0f - headHead / 100.0f);
            double endHead = head * (1.0f + headHead / 100.0f);

            /*
            Double[,] ret = new Double[4, 2]
            {
                { startFlow, head },
                { endFlow,   head },
                { flow,      startHead},
                { flow,      endHead}
            };*/

            Double[,] ret = new Double[4, 2]
            {
                { startFlow,  startHead },
                { startFlow,  endHead },
                { endFlow,    startHead},
                { endFlow,    endHead}
            };
            return ret;
        }

        /// <summary>
        /// 根据额定流量和功率的坐标，给出指定标准下的流量扬程范围
        /// </summary>
        /// <param name="flow">流量</param>
        /// <param name="head">功率</param>
        /// <param name="level">标准</param>
        /// <returns></returns>
        public static Double[,] GetFlowPowerRange(Double flow, Double power, FlowHeadLevel level)
        {
            int flowRate = (int)flowLevelTable[level];
            int powerRate = (int)powerLevelTable[level];


            double startFlow = flow * (1.0f - flowRate / 100.0f);
            double endFlow = flow * (1.0f + flowRate / 100.0f);

            double startPower = power;
            double endPower = power * (1.0f + powerRate / 100.0f);

            /*Double[,] ret = new Double[4, 2]
            {
                { startFlow, power },
                { endFlow,   power },
                { flow,      startPower},
                { flow,      endPower}
            };*/

            Double[,] ret = new Double[4, 2]
{
                { startFlow, startPower },
                { startFlow,   endPower },
                { endFlow,      startPower},
                { endFlow,      endPower}
};
            return ret;
        }

        /// <summary>
        /// 根据额定流量和效率的坐标，给出指定标准下的流量扬程范围
        /// </summary>
        /// <param name="flow">流量</param>
        /// <param name="head">效率</param>
        /// <param name="level">标准</param>
        /// <returns></returns>
        public static Double[,] GetFlowEffRange(Double flow, Double eff, FlowHeadLevel level)
        {
            int flowRate = (int)flowLevelTable[level];
            int effRate = (int)effLevelTable[level];

            double startFlow = flow * (1.0f - flowRate / 100.0f);
            double endFlow = flow * (1.0f + flowRate / 100.0f);

            double startEff = eff * (1.0f - effRate / 100.0f);
            double endEff = eff;

            /*Double[,] ret = new Double[4, 2]
            {
                { startFlow, eff },
                { endFlow,   eff },
                { flow,      startEff},
                { flow,      endEff}
            };*/

            Double[,] ret = new Double[4, 2]
            {
                { startFlow, startEff },
                { startFlow,   endEff },
                { endFlow,      startEff},
                { endFlow,      endEff}
            };
            return ret;
        }

    }
}
